package client;

import java.util.concurrent.ConcurrentHashMap;

public class FenetreConversation extends javax.swing.JFrame {

    public FenetreConversation(Client c) {
        client = c;
        initComponents(); 
        tabbedPaneConversations.removeAll();
        buttonEnvoyer.setEnabled(false);
        buttonJoindreFichier.setEnabled(false);
        buttonJouer.setEnabled(false);
        textTabs = new ConcurrentHashMap<String,javax.swing.JTextArea>();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listContacts = new javax.swing.JList();
        labelContacts = new javax.swing.JLabel();
        labelSalons = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listSalons = new javax.swing.JList();
        buttonEnvoyer = new javax.swing.JButton();
        buttonJoindreFichier = new javax.swing.JButton();
        buttonJouer = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        textChat = new javax.swing.JTextArea();
        progressBarEnvoie = new javax.swing.JProgressBar();
        labelEnvoieEnCours = new javax.swing.JLabel();
        tabbedPaneConversations = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuFichier = new javax.swing.JMenu();
        menuItemModifierCompte = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuItemDeconnexion = new javax.swing.JMenuItem();
        menuContact = new javax.swing.JMenu();
        menuItemAjouterContact = new javax.swing.JMenuItem();
        menuItemSupprimerContact = new javax.swing.JMenuItem();
        menuAide = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Fenetre de conversation");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        listContacts.setModel(new javax.swing.AbstractListModel() {
            String[] strings = client.listeDesContacts();
            public int getSize() { if(strings!=null){System.out.println("Nombre de Contacts : " + strings.length);return strings.length; }else return 0; }
            public Object getElementAt(int i) { if(strings!=null) return strings[i]; else return null; }
        });
        listContacts.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listContactsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listContacts);

        labelContacts.setText("Contacts");

        labelSalons.setText("Salons");

        listSalons.setModel(new javax.swing.AbstractListModel() {
            String[] strings = client.listeDesSalons();
            public int getSize() { if(strings!=null){System.out.println("Nombre de salons : " + strings.length);return strings.length; }else return 0; }
            public Object getElementAt(int i) { if(strings!=null) return strings[i]; else return null; }
        });
        listSalons.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listSalonsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(listSalons);

        buttonEnvoyer.setText("Envoyer");
        buttonEnvoyer.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        buttonEnvoyer.setSelected(true);
        buttonEnvoyer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEnvoyerActionPerformed(evt);
            }
        });

        buttonJoindreFichier.setText("Joindre Fichier");
        buttonJoindreFichier.setPreferredSize(new java.awt.Dimension(63, 29));

        buttonJouer.setText("Jouer");
        buttonJouer.setPreferredSize(new java.awt.Dimension(63, 29));

        textChat.setColumns(20);
        textChat.setRows(5);
        jScrollPane3.setViewportView(textChat);

        progressBarEnvoie.setPreferredSize(new java.awt.Dimension(148, 10));

        labelEnvoieEnCours.setText("Envoie en cours ...");
        labelEnvoieEnCours.setPreferredSize(new java.awt.Dimension(117, 10));

        tabbedPaneConversations.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tabbedPaneConversations.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabbedPaneConversationsMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(labelContacts)))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(labelEnvoieEnCours, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(progressBarEnvoie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(buttonJoindreFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(buttonEnvoyer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(buttonJouer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(tabbedPaneConversations, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(labelSalons)
                        .addGap(58, 58, 58))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tabbedPaneConversations, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(buttonEnvoyer, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonJoindreFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonJouer, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(progressBarEnvoie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelEnvoieEnCours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelContacts)
                            .addComponent(labelSalons))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
                            .addComponent(jScrollPane2))
                        .addContainerGap())))
        );

        tabbedPaneConversations.getAccessibleContext().setAccessibleName("");
        tabbedPaneConversations.getAccessibleContext().setAccessibleDescription("");

        menuFichier.setText("Fichier");

        menuItemModifierCompte.setText("Modifier compte");
        menuFichier.add(menuItemModifierCompte);
        menuFichier.add(jSeparator1);

        menuItemDeconnexion.setText("Deconnexion");
        menuFichier.add(menuItemDeconnexion);

        jMenuBar1.add(menuFichier);

        menuContact.setText("Contact");

        menuItemAjouterContact.setText("Ajouter Contact");
        menuContact.add(menuItemAjouterContact);

        menuItemSupprimerContact.setText("Supprimer Contact");
        menuContact.add(menuItemSupprimerContact);

        jMenuBar1.add(menuContact);

        menuAide.setText("Aide");
        jMenuBar1.add(menuAide);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonEnvoyerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEnvoyerActionPerformed
            String nomTab = (String) tabbedPaneConversations.getTitleAt(tabbedPaneConversations.getSelectedIndex());
            if(estUnSalon(nomTab))  
            {
                System.out.println("Je suis dans le handler envoyer message partie Salon : " + nomTab);
                client.envoyerMessage(textChat.getText(),nomTab,3);
            }   
            else// conversation avec un client puisque le nom de la table active n'est pas le nom d'un salon :D
            {
                System.out.println("Je suis dans le handler envoyer message partie Client : " + nomTab);
                client.envoyerMessage(textChat.getText(),nomTab,4);
            }  
            textChat.setText("");
    }//GEN-LAST:event_buttonEnvoyerActionPerformed

    public Boolean estUnSalon(String nomTab){
        Boolean trouver=false;
        for(String key : client.salons.keySet()){
                if(client.salons.get(key).getTitre().equalsIgnoreCase(nomTab))
                    trouver=true;
        }
        return trouver;
    }
    


    
    private void listSalonsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listSalonsMouseClicked
        String nomSalon = (String) listSalons.getSelectedValue();
        buttonEnvoyer.setEnabled(true);
        
        if(tabbedPaneConversations.indexOfTab(nomSalon)==-1) {
            textTabs.put(nomSalon, new javax.swing.JTextArea());
            textTabs.get(nomSalon).setEditable(false);
            tabbedPaneConversations.addTab(nomSalon, textTabs.get(nomSalon));
            tabbedPaneConversations.setSelectedIndex(tabbedPaneConversations.indexOfTab(nomSalon));
        }else{
            tabbedPaneConversations.setSelectedIndex(tabbedPaneConversations.indexOfTab(nomSalon));
        }
    }//GEN-LAST:event_listSalonsMouseClicked

    private void tabbedPaneConversationsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabbedPaneConversationsMouseClicked
        int numbtn =evt.getButton();
        if(numbtn==3){
             tabbedPaneConversations.remove(tabbedPaneConversations.getSelectedIndex());
        }
        if( tabbedPaneConversations.getTabCount()==0){
            buttonEnvoyer.setEnabled(false);
        }
    }//GEN-LAST:event_tabbedPaneConversationsMouseClicked

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        client.fermetureFenetre();
    }//GEN-LAST:event_formWindowClosing

    private void listContactsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listContactsMouseClicked
        String nomContact = (String) listContacts.getSelectedValue();
        buttonEnvoyer.setEnabled(true);
        
        if(tabbedPaneConversations.indexOfTab(nomContact)==-1) {
            textTabs.put(nomContact, new javax.swing.JTextArea());
            textTabs.get(nomContact).setEditable(false);
            tabbedPaneConversations.addTab(nomContact, textTabs.get(nomContact));
            tabbedPaneConversations.setSelectedIndex(tabbedPaneConversations.indexOfTab(nomContact));
        }else{
            tabbedPaneConversations.setSelectedIndex(tabbedPaneConversations.indexOfTab(nomContact));
        }
    }//GEN-LAST:event_listContactsMouseClicked


       

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonEnvoyer;
    private javax.swing.JButton buttonJoindreFichier;
    private javax.swing.JButton buttonJouer;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JLabel labelContacts;
    private javax.swing.JLabel labelEnvoieEnCours;
    private javax.swing.JLabel labelSalons;
    private javax.swing.JList listContacts;
    private javax.swing.JList listSalons;
    private javax.swing.JMenu menuAide;
    private javax.swing.JMenu menuContact;
    private javax.swing.JMenu menuFichier;
    private javax.swing.JMenuItem menuItemAjouterContact;
    private javax.swing.JMenuItem menuItemDeconnexion;
    private javax.swing.JMenuItem menuItemModifierCompte;
    private javax.swing.JMenuItem menuItemSupprimerContact;
    private javax.swing.JProgressBar progressBarEnvoie;
    private javax.swing.JTabbedPane tabbedPaneConversations;
    private javax.swing.JTextArea textChat;
    // End of variables declaration//GEN-END:variables
    public ConcurrentHashMap<String,javax.swing.JTextArea> textTabs;
    private Client client;

    void reInitialisationDesSalonsEtContacts() {
        this.initComponents();
    }
}
